<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>渠道推广管理</title>
    <script src="../commonCss.js"></script>
    <script src="../commonJs.js"></script>
    <link rel="stylesheet" href="../../css/common.css">
</head>

<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <div class="ctc_title">渠道推广管理</div>
    <div class="down_line"></div>
</section>

<!-- Main content -->
<section class="content" >
    <div class="row">
        <div class="col-xs-12">
            <div class="box-header">
                <form class="form-inline">
                    <div class="form-group">
                        <label>查询：</label>
                        <select  class="form-control" id="account" >
                            <option value="">全部</option>
                        </select>&nbsp;&nbsp;
                        <input type="text"  class="form-control" id="key" style="width: 250px;" placeholder="输入渠道商名称或渠道号搜索">
                    </div>
                    <div class="form-group">
                        <button type="button"  onclick="getData()" class="btn btn-block btn-primary glyphicon glyphicon-search">搜索</button>
                    </div>
                    <div style="float: right " id="right_but">
                        <div class="form-group" style="margin-right: 20px;">
                            <button type="button" onclick="addInfo()" class="btn btn-block btn-primary">新增</button>
                        </div>
                    </div>

                </form>
            </div>

            <div class="box-body" style="height:100%;">
                <table id="dataList" class="table table-bordered table-hover" >
                    <thead>
                    <tr>
                        <th>渠道商账号</th>
                        <th>渠道商</th>
                        <th>渠道号</th>
                        <th>推广链接</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody >
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</section>

<!-- 【编辑】弹出窗内容 -->
<div class="modal" id="editPanel" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" style="width: 50%;height: 40%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                    <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" align="center" id="mopTitle"></h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" onsubmit="return false" id="form">
                    <fieldset  style="margin-top:2%;">
                        <input type="hidden" id="id" name="id">
                        <div class="form-group" >
                            <label class="col-md-2 control-label red">渠道商</label>
                            <div class="col-md-10">
                                <select  class="form-control" id="agentCode" >
                                </select>
                            </div>
                        </div>
                        <div class="form-group" >
                            <label class="col-md-2 control-label" >推广模板</label>
                            <div class="col-md-10">
                                <select  class="form-control" id="modelType" >
                                    <option value="1">模板1</option>
                                    <!--<option value="2">模板2</option>-->
                                </select>
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
            <div class="modal-footer" style="text-align: center">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="confirmEditConfig();">确定</button>
            </div>
        </div>
    </div>
</div>


<!-- page script -->
<script>

    $(document).ready(function() {
        //加载数据
        initAgent();
        getData();
    });


    // 加载数据
    function getData(){
        var table = $('#dataList').DataTable({
            "destroy": true,   //可以重新初始化，用于搜索
            "lengthChange": false,
            "ordering": false,   //排序
            "info": true,		   //
            "autoWidth": false,   //宽度自动
            "paging": true,      //分页
            "searching": false,  //全局搜索关闭
            "processing": true,  //开启显示‘正在加载.../processing’
            "serverSide":true,    //true代表后台处理分页，false代表前台处理分页
            "bPaginate": false, //开关，是否显示分页器
            "bLengthChange": false, //开关，是否显示每页显示多少条数据的下拉框
            "ajax" :  {
                url:"/agent/getAgentChannelList",
                async : false,
                type:"POST",
                data:{
                    key : $("#key").val(),
                    account : $("#account").val()
                }
            },
            //参数显示,"visible": false,
            "columns": [
                {"data": "account", 'sClass': "text-center",'width': '4%'},
                {"data": "name", 'sClass': "text-center",'width': '5%'},
                {"data": "channelNum", 'sClass': "text-center",'width': '4%'},
                {"data": "downloadUrl", 'sClass': "text-center",'width': '10%',
                    "render": function (data, type, full, meta ) {
                        var str = "";
                        if(data != ""){
                            str =  '<span style="word-wrap:break-word;white-space:normal;word-break:break-all;">' + data + '</span>';
                            str += "&nbsp;"+"<button class='btn' style='padding: 1px 1px;' data-clipboard-text='"+data+"'>复制</button>";
                        }
                        return str;
                    }
                },
                {"data": "createTime", 'sClass': "text-center",'width': '5%'},
                {"data": "id" , 'sClass': "text-center tx" ,'width':'3%',
                    "render": function (data, type, full, meta ) {
                            var str = "<a href = '#' title='删除' onclick=deleteData(" + data +");><i class='\glyphicon glyphicon-trash'></i></a>";
                            return str;
                    }
                }
            ],

            //是否开启主题
            "bJQueryUI": true,
            "oLanguage": {    // 语言设置
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "sZeroRecords": "没有检索到数据",
                "sSearch": "检索:",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                }
            }
        });
    }


    //初始化下来选择渠道商
    function initAgent() {
        $.ajax({
            type:"POST",
            url:"/agent/getAllAgent",
            async : true,//默认就是true
            dataType:"json",
            data:{},
            success : function(res) {
                for (var i = 0; i <res.data.length ; i++) {
                    $("#account").append("<option value='"+res.data[i].account +"'>"+res.data[i].account+"</option>");
                }
            }
        });
    }

    //打开新增窗口
    function addInfo() {
        $("#agentCode").empty();
        $("#modelType").val(1);

        //初始化渠道商
        $.ajax({
            type:"POST",
            url:"/agent/getAllAgent",
            async : true,//默认就是true
            dataType:"json",
            data:{},
            success : function(res) {
                for (var i = 0; i <res.data.length ; i++) {
                    $("#agentCode").append("<option value='"+res.data[i].code +"'>"+res.data[i].account+"</option>");
                }
            }
        });

        $("#mopTitle").text("新增渠道推广");
        $("#editPanel").modal("toggle");//弹窗打开
    }

    //保存
    function confirmEditConfig(){
        var agentCode = $("#agentCode").val();
        var modelType = $("#modelType").val();

        if(agentCode == ""){
            layer.msg("请选择渠道商！");
            return ;
        }
        if(modelType == ""){
            layer.msg("请选择模板类型！");
            return ;
        }

        $.ajax({
            type : 'post',
            url : "/agent/addAgentChannel",
            async : true,//默认就是true
            dataType:"json",
            data:{
                code : agentCode,
                modelType : modelType
            },
            success : function(data) {
                layer.msg(data.msg, {shift: -1, time: 100}, function(){
                    location.href = "channelList.html";
                });
            }
        });
    }

    //删除
    function deleteData(id){
        if(confirm("确定要删除吗？")){
            $.ajax({
                type:"POST",
                url:"/agent/delAgentChannel",
                async : true,//默认就是true
                dataType:"json",
                data:{id : id},
                success : function(data) {
                    layer.msg(data.msg, {shift: -1, time: 500}, function(){
                        location.href = "channelList.html";
                    });

                }
            });
        }
    }
    var clipboard = new Clipboard('.btn');

    clipboard.on('success', function(e) {
        layer.msg("复制成功", {shift: -1, time: 500}, function(){
        });
    });

    clipboard.on('error', function(e) {
        layer.msg("复制失败", {shift: -1, time: 500}, function(){
        });
    });

</script>
</body>
</html>
